package com.zzptc.myk.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.zzptc.myk.myapplication.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Button btn_login;
    Button btn_reg;
    EditText et_name;
    EditText et_pwd;
    Button btn_aj1;
    Button btn_aj2;
    Switch aSwitch;
    private static final String TAG = "MainActivity";
    List<User> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//实例化控件
        event();//处理事件

    }
    public void init(){
        btn_login = findViewById(R.id.btn_login);
        btn_reg = findViewById(R.id.btn_reg);
        et_name = findViewById(R.id.et_name);
        et_pwd = findViewById(R.id.et_pwd);
        btn_aj1 = findViewById(R.id.btn_aj1);
        btn_aj2 = findViewById(R.id.btn_aj2);
    }
    public void event(){
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

                Intent intent=new Intent(MainActivity.this,RegActivity.class);
                startActivityForResult(intent,1);
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                });
        //Toast.makeText(this, "账号密码不正确", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==2){
            String uname = data.getStringExtra("uname");
            String pwd = data.getStringExtra("pwd");
            Log.d(TAG,""+pwd);
            Log.d(TAG,""+uname);
            //封装数据
            User user =new  User(uname,pwd);
            //注册
            reg(user);
            //Toast.makeText(this, ""+uname, Toast.LENGTH_SHORT).show();
        }
    }

    public void reg(User user){
        SharedPreferences s=getSharedPreferences("data",MODE_PRIVATE);
        SharedPreferences.Editor editor=s.edit();
        editor.putString("name",user.getUsername());
        editor.putString("age",user.getPassword());
        editor.apply();
        //判断集合中有没有对象或有没有该对象
       /* if(list.size()==0){
            list.add(user);
            Log.d(TAG,list.size()+""+user.getUsername());
        }else {
            for (User s:list){
                if(s.getUsername().equals(user.getUsername())){
                    Toast.makeText(this, "该账号已被注册", Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    list.add(user);
                    Log.d(TAG,list.size()+"===="+user.getUsername());
                }
            }
        }*/

    }
}
